//
//  DetailViewController.m
//  EconomyDemo
//
//  Created by qianfeng01 on 15-7-10.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailModel.h"
#import "AFNetworking.h"
#import "GDataXMLNode.h"
#import "HeaderView.h"
#import "FootView.h"
#import "ArticleCell.h"

#define kScreenSize [UIScreen mainScreen].bounds.size
#define kUrl @"http://ipadnews.caijing.com.cn/api/1.0/articles.php?bundleId=cn.com.caijing.android&platform=android4.4.2&appVer=2.4.1&network=wifi&action=detail&id=%d"

@interface DetailViewController () <UITableViewDataSource,UITableViewDelegate>
{
    AFHTTPRequestOperationManager *_manager;
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSString *_str;
    HeaderView *_headerView;
    FootView *_footView;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic,strong) HeaderView *headerView;
@property (nonatomic,strong) FootView *footView;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray=[[NSMutableArray alloc] init];
    
    [self creatTableView];
    [self loadData];
}
-(void)creatTableView{
    self.headerView=[[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil] lastObject];
    self.footView=[[[NSBundle mainBundle] loadNibNamed:@"FootView" owner:nil options:nil] lastObject];
    
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    self.tableView.tableHeaderView=self.headerView;
    self.tableView.tableFooterView=self.footView;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ArticleCell" bundle:nil] forCellReuseIdentifier:@"ArticleCell"];
    [self.view addSubview:_tableView];
}
-(void)loadData{
    _manager=[[AFHTTPRequestOperationManager alloc] init];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof(self) weakSelf=self;
    NSString *url=[NSString stringWithFormat:kUrl,(int)self.articleId.intValue];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"成功下载");
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            DetailModel *model=[[DetailModel alloc] init];
                        model.title=dict[@"title"];
                        model.author=dict[@"author"];
                        model.content=dict[@"content"];
                        model.description=dict[@"description"];
                        model.keywords=dict[@"keywords"];
            [weakSelf.dataArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    
}
-(NSString *)xmlAnalysis:(NSString *)str{
    NSArray *arr1=[str componentsSeparatedByString:@"&ldquo;"];
    NSString *str1=[arr1 componentsJoinedByString:@"“"];
    NSArray *arr2=[str1 componentsSeparatedByString:@"&rdquo;"];
    NSString *str2=[arr2 componentsJoinedByString:@"”"];
    NSArray *arr3=[str2 componentsSeparatedByString:@"&middot;"];
    NSString *str3=[arr3 componentsJoinedByString:@"."];
    NSArray *arr4=[str3 componentsSeparatedByString:@"&mdash;"];
    NSString *str4=[arr4 componentsJoinedByString:@"--"];
    NSArray *arr5=[str4 componentsSeparatedByString:@"&hellip;"];
    NSString *str5=[arr5 componentsJoinedByString:@"……"];
    NSArray *arr6=[str5 componentsSeparatedByString:@"&lsquo;"];
    NSString *str6=[arr6 componentsJoinedByString:@"‘"];
    NSArray *arr7=[str6 componentsSeparatedByString:@"&rsquo;"];
    NSString *str7=[arr7 componentsJoinedByString:@"’"];
    NSArray *arr8=[str7 componentsSeparatedByString:@"&nbsp;"];
    NSString *str8=[arr8 componentsJoinedByString:@""];
    NSArray *arr9=[str8 componentsSeparatedByString:@"&ndash;"];
    NSString *str9=[arr9 componentsJoinedByString:@"-"];
    NSArray *arr10=[str9 componentsSeparatedByString:@"&"];
    NSString *str10=[arr10 componentsJoinedByString:@""];
    
    NSMutableString *mStr=[[NSMutableString alloc] initWithString:str10];
    [mStr appendString:@"</root>"];
    [mStr insertString:@"<root>" atIndex:0];
    NSData *data=[mStr dataUsingEncoding:NSUTF8StringEncoding];
    GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    GDataXMLElement *root=[doc rootElement];
    NSArray *arr=[root elementsForName:@"p"];
    NSMutableArray *mArr=[[NSMutableArray alloc] init];
    for (GDataXMLElement *element in arr) {
        
        [mArr addObject:[element stringValue]];
    }
    NSString *des=[mArr componentsJoinedByString:@""];
    return des;
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)
tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ArticleCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ArticleCell" forIndexPath:indexPath];
    
    DetailModel *model=self.dataArray[indexPath.row];
     NSString *content=[self xmlAnalysis:model.content];
    self.headerView.titleLabel.text=model.title;
    self.headerView.authorLabel.text=model.author;
    self.footView.FootLabel.text=model.keywords;
    //cell.content=content;
    cell.abstractLabel.text=model.description;
    cell.articleLabel.text=content;
    return cell;
}




@end
